package com.example.mygif;

import android.graphics.Bitmap;
import android.util.Log;

public class GifHandler {

    private static boolean DEBUG = true;
    private final static String TAG = "GifHandler";

    private static long mPointer;

    public void loadFile(String path) {

        mPointer = nativeLoadFile(path);
        if (DEBUG) {
            Log.i(TAG, "pointer is " + mPointer);
        }
    }

    /**
     * 获取图片的宽度
     *
     * @return 宽度
     * @throws Exception 没有加载图片异常
     */
    public int getWith() throws Exception {
        safeCheck();
        return nativeGetWidth(mPointer);
    }

    /**
     * 获取图片的高度
     *
     * @return 图片的高度
     * @throws Exception 没有加载图片异常
     */
    public int getHeight() throws Exception {
        safeCheck();
        return nativeGetHeight(mPointer);
    }

    /**
     * 在bitmap容器中绘制图像内容
     *
     * @param bitmap bitmap 容器
     */
    public int drawFrame(Bitmap bitmap) throws Exception {
        safeCheck();
        return nativeDrawFrame(bitmap, mPointer);
    }


    private void safeCheck() throws Exception {
        if (mPointer == 0) {
            throw new Exception("没有加载gif图片");
        }
    }

    private native long nativeLoadFile(String path);

    private native int nativeGetWidth(long pointer);

    private native int nativeGetHeight(long pointer);

    private native int nativeDrawFrame(Bitmap bitmap, long pointer);

    public native void test();

    public native void testBitmap(Bitmap bitmap);


}
